<template>
  <el-dialog title="分配角色" :visible.sync="showRoleDialog">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{
          item.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="showRoleDialog = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  data() {
    return {
      showRoleDialog: false,
      list: [],
      roleIds: [], // 角色Id列表
      userId: null
    }
  },
  created() {
  },
  methods: {
    async getRoleId(id) {
      const res = await getUserDetailById(id)
      this.roleIds = res.roleIds || []
      this.userId = id
    },
    // 确定按钮
    async btnOk() {
      // 设置角色信息
      await assignRoles({
        id: this.userId,
        roleIds: this.roleIds
      })
      this.$message.success('关联成功')
      this.showRoleDialog = false
    //   console.log(res)
    },
    async initData() {
      const { rows } = await getRoleListApi({
        page: 1, pagesize: 100
      })
      this.list = rows
    }
  }
}
</script>
